package server.jdbcdao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import server.core.Reached;
import server.core.User;
import server.dao.ReachedDAO;
import server.idatasource.DataSourceFactory;
import server.idatasource.IReachedDataSource;

public class ReachedJdbcDAO implements ReachedDAO {

	private List<Reached> makeReachedList(List<List<String>> result) {
		Iterator<List<String>> it1 = result.iterator();
		Reached reached;
		List<Reached> reachedList = new ArrayList<Reached>();

		while (it1.hasNext()) {
			Iterator<String> it2 = it1.next().iterator();
			reached = new Reached();
			reached.setUsername(it2.next());
			reached.setCheckpointId(Integer.parseInt(it2.next()));
			reached.setDate(it2.next());
			reachedList.add(reached);
		}
		return reachedList;
	}

	public List<Reached> getAllReached() {
		DataSourceFactory ds = DataSourceFactory.getInstance();
		IReachedDataSource reachedData = ds.getReachedDataSource();

		List<List<String>> result = reachedData.getAllReached();
		List<Reached> reachedList = makeReachedList(result);

		return reachedList;
	}

	@Override
	public List<Reached> getAllReachedByUser(User user) {
		DataSourceFactory ds = DataSourceFactory.getInstance();
		IReachedDataSource reachedData = ds.getReachedDataSource();

		List<List<String>> result = reachedData.getAllReached();
		List<Reached> reachedList = makeReachedList(result);

		return reachedList;
	}

	@Override
	public List<Reached> getAllReachedFromDate(String date) {
		DataSourceFactory ds = DataSourceFactory.getInstance();
		IReachedDataSource reachedData = ds.getReachedDataSource();

		List<List<String>> result = reachedData.getAllReached();
		List<Reached> reachedList = makeReachedList(result);

		return reachedList;
	}

	@Override
	public List<Reached> getAllReachedToDate(String date) {
		DataSourceFactory ds = DataSourceFactory.getInstance();
		IReachedDataSource reachedData = ds.getReachedDataSource();

		List<List<String>> result = reachedData.getAllReached();
		List<Reached> reachedList = makeReachedList(result);

		return reachedList;
	}

	@Override
	public String insertReached(Reached reached) {
		DataSourceFactory ds = DataSourceFactory.getInstance();
		IReachedDataSource reachedData = ds.getReachedDataSource();

		return reachedData.insertReached(reached.getUsername(), reached.getCheckpointId(), reached.getDate());
	}

	@Override
	public String deleteReached(String username, int checkpointId) {
		DataSourceFactory ds = DataSourceFactory.getInstance();
		IReachedDataSource reachedData = ds.getReachedDataSource();

		return reachedData.deleteReached(username, checkpointId);
	}

	@Override
	public String updateReached(Reached reached) {
		DataSourceFactory ds = DataSourceFactory.getInstance();
		IReachedDataSource reachedData = ds.getReachedDataSource();

		return reachedData.updateReached(reached.getUsername(), reached.getCheckpointId(), reached.getDate());
	}
}
